﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Resources;

namespace MyConomics
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //// Form loads
            // Load gadgets
            LoadAllModules();
        }

        private void LoadAllModules()
        {
            // Load each gadget in the gadget folder
            foreach (string gadgetfile in Directory.GetFiles(@"E:\MyConomics Gadgets\"))
            {
                LoadModule(gadgetfile);
            }
        }

        private void LoadModule(string path)
        {
            //// Function to Load Gadget

            // Loads the specified Assembly
            Assembly assembly = Assembly.LoadFile(path);

            // Extracts the type of the Control from the Assembly
            Type gadgetType = assembly.GetType(assembly.GetTypes()[0].Namespace + "." + assembly.GetTypes()[0].Name);
            
            // Creates and instance of the Control in the Assembly
            Control module = (Control)Activator.CreateInstance(gadgetType);

            // Checks if the control is a Module or Gadget by refering to a private field in the object called IsModule
            int value = (int)gadgetType.InvokeMember("IsModule", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic, null, module, null);

            if (value == 1)
            {
                // If the Loaded File is a Module do this
                // Loads the Icon for the Module
                //assembly.GetManifestResourceStream("Icon.png");

                PictureBox workbutton = new PictureBox();
                Stream rstream = assembly.GetManifestResourceStream("CalendarControl.Properties.Resources.moduleicon");
                workbutton.Image = Image.FromStream(rstream);
                leftPanel.Controls.Add(workbutton);
                

                // Sets the padding of the Module
                ((UserControl)module).Margin = new Padding(0, 4, 0, 4);

                // Gets the name of the Module from a field in the file
                string moduleName = (string)gadgetType.InvokeMember("ModuleName", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic, null, module, null);

                // Adds a new tab to the tabpanel with the new Module
                mainTabPanel.TabPages.Add(moduleName);

                // Module settings
                module.Dock = DockStyle.Fill;
                ((UserControl)module).BorderStyle = BorderStyle.FixedSingle;

                // Places the Module on the TabPanel
                mainTabPanel.TabPages[mainTabPanel.TabPages.Count - 1].Controls.Add(module);
            }
            else
            {
                // If the Loaded File is a Gadget do this
                // Sets the margin
                ((UserControl)module).Margin = new Padding(0, 4, 0, 4);

                //((UserControl)gadget).BorderStyle = BorderStyle.FixedSingle;

                // Creates a new panel for the new Gadget
                Panel pan = new Panel();
                pan.Width = 242;
                pan.Height = ((UserControl)module).Height + 24;
                pan.BorderStyle = BorderStyle.FixedSingle;

                Label gadgetHeader = new Label();
                gadgetHeader.Text = module.Tag.ToString();
                gadgetHeader.AutoSize = false;
                gadgetHeader.Dock = DockStyle.Top;
                gadgetHeader.TextAlign = ContentAlignment.MiddleCenter;
                gadgetHeader.BackColor = Color.LightBlue;
                gadgetHeader.BorderStyle = BorderStyle.FixedSingle;

                gadgetHeader.MouseEnter += new EventHandler(gadgetHeader_MouseEnter);
                gadgetHeader.MouseLeave += new EventHandler(gadgetHeader_MouseLeave);

                module.Dock = DockStyle.Fill;

                // Adds the header and the newly loaded gadget to the panel
                pan.Controls.Add(gadgetHeader);
                module.Top = gadgetHeader.Height;
                pan.Controls.Add(module);
                module.BringToFront();

                // Returns the new Control
                rightPanel.Controls.Add(pan);
            }
        }

        private void gadgetHeader_MouseEnter(object sender, EventArgs e)
        {
            ((Control)sender).BackColor = Color.AliceBlue;
        }

        private void gadgetHeader_MouseLeave(object sender, EventArgs e)
        {
            ((Control)sender).BackColor = Color.LightBlue;
        }
    }
}
